﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 日志管理类。
 * 
 * 最后修改：2011-10-31
 * ************************************************************/
namespace Mozlite.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Web;
    using Mozlite.Common.Configuration;

    /// <summary>
    /// 日志管理类。
    /// </summary>
    public class Logs
    {
        private Logs()
        {
        }
        /// <summary>
        /// 移除过期日期的日志。
        /// </summary>
        /// <param name="settingsID">网站设置ID。</param>
        /// <param name="date">过期时间。</param>
        public static void Clear(int settingsID, DateTime date)
        {
            CommonDataProvider.Current.ClearLogs(settingsID, date);
        }
        /// <summary>
        /// 写入调试状态下的信息。
        /// </summary>
        /// <param name="message">描述。</param>
        /// <param name="category">分类。</param>
        /// <param name="eventID">事件ID。</param>
        /// <param name="source">来源。</param>
        /// <param name="settingsID">网站设置ID。</param>
        [Conditional("DEBUG")]
        public static void Debug(string message, string category, EventID eventID, string source, int settingsID = -1)
        {
            Write(message, category, eventID, LogType.Debug, source, settingsID);
        }
        /// <summary>
        /// 写入普通信息。
        /// </summary>
        /// <param name="message">描述。</param>
        /// <param name="category">分类。</param>
        /// <param name="eventID">事件ID。</param>
        /// <param name="source">来源。</param>
        /// <param name="settingsID">网站设置ID。</param>
        public static void Info(string message, string category, EventID eventID, string source, int settingsID = -1)
        {
            Write(message, category, eventID, LogType.Information, source, settingsID);
        }
        /// <summary>
        /// 写入堆栈信息。
        /// </summary>
        /// <param name="source">来源。</param>
        /// <param name="category">分类。</param>
        /// <param name="settingsID">网站设置ID。</param>
        public static void StackTrack(string source, string category = "Stack Track", int settingsID = -1)
        {
            Write(new StackTrace().ToString(), category, EventID.StackTrack, LogType.Information, source, settingsID);
        }
        /// <summary>
        /// 写入错误信息。
        /// </summary>
        /// <param name="message">描述。</param>
        /// <param name="category">分类。</param>
        /// <param name="eventID">事件ID。</param>
        /// <param name="ex">错误对象。</param>
        /// <param name="source">来源，如果为<c>null</c>，则使用错误的来源。</param>
        /// <param name="settingsID">网站设置ID。</param>
        public static void Error(string message, string category, EventID eventID, Exception ex = null, string source = null, int settingsID = -1)
        {
            if (source == null && ex != null)
                source = ex.Source;
            if (ex == null)
                message = string.Format("{0}\r\n{1}\r\n{2}\r\n{3}", message, ex.GetType(), ex.Message, ex.StackTrace);
            Write(message, category, eventID, LogType.Warning, source, settingsID);
        }
        /// <summary>
        /// 写入警告信息。
        /// </summary>
        /// <param name="message">描述。</param>
        /// <param name="category">分类。</param>
        /// <param name="eventID">事件ID。</param>
        /// <param name="ex">错误对象。</param>
        /// <param name="source">来源，如果为<c>null</c>，则使用错误的来源。</param>
        /// <param name="settingsID">网站设置ID。</param>
        public static void Warn(string message, string category, EventID eventID, Exception ex = null, string source = null, int settingsID = -1)
        {
            if (source == null && ex != null)
                source = ex.Source;
            if(ex == null)
                message = string.Format("{0}\r\n{1}\r\n{2}", message, ex.GetType(), ex.Message);
            Write(message, category, eventID, LogType.Warning, source, settingsID);
        }
        /// <summary>
        /// 写入日志。
        /// </summary>
        /// <param name="entry">当前日志实体类。</param>
        public static void Write(LogInfo entry)
        {
            try
            {
                if (string.IsNullOrEmpty(entry.MachineName))
                {
                    entry.MachineName = Environment.MachineName;
                }
                if (!string.IsNullOrEmpty(entry.Message))
                {
                    entry.Message = HttpUtility.HtmlEncode(entry.Message.Trim());
                }
                CommonDataProvider.Current.WriteLog(entry);
            }
            catch
            {
            }
        }
        /// <summary>
        /// 写入日志。
        /// </summary>
        /// <param name="message">描述。</param>
        /// <param name="category">分类。</param>
        /// <param name="eventID">事件ID。</param>
        /// <param name="logType">事件类型。</param>
        /// <param name="source">来源。</param>
        /// <param name="settingsID">网站设置ID。</param>
        public static void Write(string message, string category, EventID eventID, LogType logType, string source, int settingsID = -1)
        {

            if (settingsID == -1)
                settingsID = SiteSettings.Current.SettingsID;
            Write(new LogInfo{SettingsID = settingsID, Message = message, Category=category, EventID = eventID, LogType = logType, Source = source});
        }
        /// <summary>
        /// 分页加载事件日志。
        /// </summary>
        /// <param name="query">日志查询实例。</param>
        /// <param name="totalSize">返回所有记录数。</param>
        /// <returns>返回当前页码的记录事件日志。</returns>
        public static List<LogInfo> LoadLogs(LogQuery query,out int totalSize)
        {
            return CommonDataProvider.Current.LoadLogs(query, out totalSize);
        }
        /// <summary>
        /// 通过ID获取当前日志信息。
        /// </summary>
        /// <param name="logID">当前日志编号。</param>
        /// <returns>返回当前日志信息。</returns>
        public static LogInfo GetLog(int logID) {
            return CommonDataProvider.Current.GetLog(logID);
        }
        /// <summary>
        /// 删除事件日志。
        /// </summary>
        /// <param name="settingsID">网站设置ID。</param>
        /// <param name="logIDs">事件日志ID集合，以","分割。</param>
        /// <returns>返回删除结果。</returns>
        public static bool DeleteLogs(int settingsID, string logIDs)
        {
            return CommonDataProvider.Current.DeleteLogs(settingsID, logIDs);
        }
    }
}
